﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADEVSO.Util;
using SmartParking.Bussiness;
using SmartParking.Data.Entities;
using SmartParking.Web.Models;

namespace SmartParking.Controllers
{
    public class HomeController : BaseController
    {
        private bool IsMobileDevice()
        {
            string returnUrl = Request.QueryString["ReturnUrl"];
            if (Request.Browser.IsMobileDevice || ((returnUrl != null) && returnUrl.StartsWith("/Mobile/", StringComparison.OrdinalIgnoreCase)))
            {
                return true;
            }
            return false;
        }

        public ActionResult Index()
        {
            if (IsMobileDevice())
            {
                return Redirect(Request.Url + "/Mobile/");
            }
            else
            {
                return View();
            }
        }

        public ActionResult PayPalTest()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult How()
        {
            return View();
        }

        public ActionResult FAQs()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }

        public ActionResult Terms()
        {
            return View();
        }

        public ActionResult JoinBussiness()
        {
            return View();
        }

        public ActionResult Reservations()
        {
            if (IsMobileDevice())
            {
                return RedirectToAction("Reservations", "Mobile", new ReservationListModel(HttpContext, ReservationListModel.ReservationListType.Session));
            }
            else
            {
                return View(new ReservationsModel(HttpContext));
            }
        }

        public ActionResult Test()
        {
            return View();
        }

        [HttpPost]
        public ActionResult RefreshModel(ReservationModel model)
        {
            model.Refresh();
            return Json(model);
        }

        [HttpPost]
        public ActionResult Reserve(ReservationModel model)
        {
            var result = new ReservationsModel(HttpContext);
            if (ModelState.IsValid)
            {
                try
                {
                    var reservation = ReservationModel.CreateOrderDetail(model);
                    ReservationsManager.AddLiveReservation(HttpContext.ApplicationInstance.Application, HttpContext.Session, Convert.ToInt32(Profile.PropertyValues[""]), reservation);
                    result.RefreshReservations();
                    result.ReservationsHtml = RenderPartialViewToString("ReservationsList", result.Reservations);
                }
                catch (Exception ex)
                {
                    result.ErrorMessage = ex.Message;
                }
            }            
            return Json(result);
        }        

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var result = new ReservationsModel(HttpContext);
            try
            {
                ReservationsManager.RemoveLiveReservations(HttpContext.ApplicationInstance.Application, HttpContext.ApplicationInstance.Session.SessionID, id);
                result.RefreshReservations();
                result.ReservationsHtml = RenderPartialViewToString("ReservationsList", result.Reservations);
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
            }
            return Json(result);
        }
    }
}